using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using HexTileBoard;

namespace ScreenManagement
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class HowToPlayScreen : MenuScreen
    {
        #region Fields
        ContentManager content;
        SpriteFont spriteFont;

        Texture2D gradient;
        Texture2D texHill, texGrass, texTree;
        Texture2D texInfluenceO1, texInfluenceO2, texInfluenceO3, texInfluenceD, texBridge;
        Texture2D texCompare;
        Texture2D texInfluenceCompare;
        Texture2D texClay, texIron, texStone, texWheat, texWood;

        int yRoot = 92;
        double timeSinceIncrement = 0.0f;
        bool exiting = false;// this is to hide images as the screen is fading on "ExitScreen"

        // Paging system
        int currentPage = 1;
        int maxPages = 6;
        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public HowToPlayScreen()
            : base("")
        {

            //MenuEntry back = new MenuEntry("Back");

            //back.Selected += OnCancel;
            
            //MenuEntries.Add(back);
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            spriteFont = content.Load<SpriteFont>("Fonts/gameFontSmall");

            gradient = content.Load<Texture2D>("Textures/Screens/Gradient");

            texHill = content.Load<Texture2D>("Textures/Icons/texHill");
            texGrass = content.Load<Texture2D>("Textures/Icons/texGrass");
            texTree = content.Load<Texture2D>("Textures/Icons/texTree");

            texInfluenceD = content.Load<Texture2D>("Textures/Icons/texInfDefensive");
            texInfluenceO1 = content.Load<Texture2D>("Textures/Icons/texInfO1");
            texInfluenceO2 = content.Load<Texture2D>("Textures/Icons/texInfO2");
            texInfluenceO3 = content.Load<Texture2D>("Textures/Icons/texInfO3");
            texCompare = content.Load<Texture2D>("Textures/Icons/compare");
            texInfluenceCompare = content.Load<Texture2D>("Textures/Icons/texCompareInfluence");
            texClay = content.Load<Texture2D>("Textures/Resources/clay_s");
            texIron = content.Load<Texture2D>("Textures/Resources/iron_s");
            texStone = content.Load<Texture2D>("Textures/Resources/stone_s");
            texWheat = content.Load<Texture2D>("Textures/Resources/wheat_s");
            texWood = content.Load<Texture2D>("Textures/Resources/wood_s");
            texBridge = content.Load<Texture2D>("Textures/Icons/texBridge");
            //howtoPawn1 = content.Load<Texture2D>("Textures/howtopawn1");
            //howtoPawn2 = content.Load<Texture2D>("Textures/howtopawn2");
            //howtoRook = content.Load<Texture2D>("Textures/howtorook");
            //howtoKnight = content.Load<Texture2D>("Textures/howtoknight");
            //howtoBishop = content.Load<Texture2D>("Textures/howtobishop");
            //howtoQueen = content.Load<Texture2D>("Textures/howtoqueen");
            //howtoKing = content.Load<Texture2D>("Textures/howtoking");
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (timeSinceIncrement <= 0.5f)
                timeSinceIncrement += gameTime.ElapsedGameTime.TotalSeconds;

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            if (!exiting)
            {
                this.ScreenManager.SpriteBatch.Begin();

                this.ScreenManager.SpriteBatch.Draw(gradient, new Rectangle(0, 0, 1280, 720), new Color(0.7f, 0.7f, 0.7f, 0.7f));

                switch (currentPage)
                {
                    case 1:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The challenge in this game is to occupy as much of the board as possible! You can win by\n" +
                                                                              "   possessing a majority (60%) or maxing out your resources (99 a piece). There are\n" +
                                                                              "        structures and actions you can use to claw your way to the top.", new Vector2(130, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The challenge in this game is to occupy as much of the board as possible! You can win by\n" +
                                                                              "   possessing a majority (60%) or maxing out your resources (99 a piece). There are\n" +
                                                                              "        structures and actions you can use to claw your way to the top.", new Vector2(128, yRoot), Color.White);

                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The basic idea is to construct and upgrade your own towers, while simultaneously knocking\n" +
                                                                              "down your opponent's. Both actions require certain resources that can be collected as \n" +
                                                                              "long as you influence the tiles where they are located. This is visually indicated by your \n" +
                                                                              "team color and elevation - the higher the elevation, the more resources you get from the\n" +
                                                                              "            tile. The greatest influence you can exert is a value of 3.", new Vector2(130, yRoot + spriteFont.LineSpacing * 4 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The basic idea is to construct and upgrade your own towers, while simultaneously knocking\n" +
                                                                              "down your opponent's. Both actions require certain resources that can be collected as \n" +
                                                                              "long as you influence the tiles where they are located. This is visually indicated by your \n" +
                                                                              "team color and elevation - the higher the elevation, the more resources you get from the\n" +
                                                                              "            tile. The greatest influence you can exert is a value of 3.", new Vector2(128, yRoot + spriteFont.LineSpacing * 4), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texInfluenceCompare, new Rectangle(370, yRoot + spriteFont.LineSpacing * 10, 540, 240), Color.White);
                        break;
                    case 2:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "In order to build towers or bridges, upgrade towers, or attack your opponents, you need\n" +
                                                                              "resources. There are four (4) building resources (clay, stone, wheat and wood) and one (1)\n" +
                                                                              "for attacking (iron). They can be gathered at the end of every turn if they are in your\n" +
                                                                              "area of influence, or they can be traded for when they are available, by accessing the\n" +
                                                                              "trade screen on your turn, using DPad up.Their icons and abbreviations are shown below:", new Vector2(130, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "In order to build towers or bridges, upgrade towers, or attack your opponents, you need\n" +
                                                                              "resources. There are four (4) building resources (clay, stone, wheat and wood) and one (1)\n" +
                                                                              "for attacking (iron). They can be gathered at the end of every turn if they are in your\n" +
                                                                              "area of influence, or they can be traded for when they are available, by accessing the\n" +
                                                                              "trade screen on your turn, using DPad up.Their icons and abbreviations are shown below:", new Vector2(128, yRoot), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texClay, new Rectangle(190, (72 * 4), 80, 64), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texIron, new Rectangle(190 * 2, (72 * 4), 80, 64), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texStone, new Rectangle(190 * 3, (72 * 4), 80, 64), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texWheat, new Rectangle(190 * 4, (72 * 4), 80, 64), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texWood, new Rectangle(190 * 5, (72 * 4), 80, 64), Color.White);

                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Clay (C)", new Vector2(190 - 18, 72 * 5 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Clay (C)", new Vector2(190 - 20, 72 * 5), Color.White);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Iron (I)", new Vector2(190 * 2 - 18, 72 * 5 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Iron (I)", new Vector2(190 * 2 - 20, 72 * 5), Color.White);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Stone (S)", new Vector2(190 * 3 - 18, 72 * 5 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Stone (S)", new Vector2(190 * 3 - 20, 72 * 5), Color.White);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Wheat (H)", new Vector2(190 * 4 - 18, 72 * 5 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Wheat (H)", new Vector2(190 * 4 - 20, 72 * 5), Color.White);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Wood (W)", new Vector2(190 * 5 - 18, 72 * 5 + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Wood (W)", new Vector2(190 * 5 - 20, 72 * 5), Color.White);

                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "It requires one (1) iron to attack and reduce an opposing influence tower or bridge that\n" +
                                                                              "    is in range. Buildings can be constructed according to the costs on the next page;\n" +
                                                                              "     influence towers must be built ongrass or trees, defensive towers on hills, \n" +
                                                                              "                                   and bridges on water.", new Vector2(130, spriteFont.LineSpacing * 12 + 66), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "It requires one (1) iron to attack and reduce an opposing influence tower or bridge that\n" +
                                                                              "    is in range. Buildings can be constructed according to the costs on the next page;\n" +
                                                                              "     influence towers must be built ongrass or trees, defensive towers on hills, \n" +
                                                                              "                                   and bridges on water.", new Vector2(128, spriteFont.LineSpacing * 12 + 64), Color.White);
                        break;
                    case 3:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Defensive structures cannot be upgraded, have short firing range, and are more expensive, \n" +
                                                                              "    but cannot be destroyed. They must be built on hills and are useful for securing \n" +
                                                                              "  bottlenecks and clay. They provide 1 influence to neighboring tiles and cost 20 wood,\n" +
                                                                              "                                       stone and clay.", new Vector2(130, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Defensive structures cannot be upgraded, have short firing range, and are more expensive, \n" +
                                                                              "    but cannot be destroyed. They must be built on hills and are useful for securing \n" +
                                                                              "  bottlenecks and clay. They provide 1 influence to neighboring tiles and cost 20 wood,\n" +
                                                                              "                                       stone and clay.", new Vector2(128, yRoot), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texHill, new Rectangle(360, yRoot + (spriteFont.LineSpacing * 5), 180, 180), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texInfluenceD, new Rectangle(710, yRoot + (spriteFont.LineSpacing * 5), 180, 180), Color.White);

                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Since water tiles block the spread of influence, adding a bridge will allow the influence  \n" +
                                                                              "to spread. They require 10 wood and 5 clay/stone to construct and can be used by anyone.", new Vector2(130, yRoot + spriteFont.LineSpacing * 5 + 202), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Since water tiles block the spread of influence, adding a bridge will allow the influence  \n" +
                                                                              "to spread. They require 10 wood and 5 clay/stone to construct and can be used by anyone.", new Vector2(128, yRoot + spriteFont.LineSpacing * 5 + 200), Color.White);
                        
                        this.ScreenManager.SpriteBatch.Draw(texBridge, new Rectangle(570, 510, 140, 140), Color.White);
                        break;
                    case 4:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Influence structures are cheaper and can be built on grass or trees, but can be attacked\n" +
                                                                              "/destroyed. They can, however, be upgraded to increase nearby influence, and can attack\n" +
                                                                              "  further once upgraded. They provide 1 to 3 influence to neighboring tiles and cost\n" +
                                                                              "5 wood/stone/clay, 2 wheat for level 1, 10 wood/clay, 5 stone, and 2 wheat for level 2,\n" +
                                                                              "              and 20 wood, 10 clay, 15 stone, 5 wheat for level 3.", new Vector2(128 + 2, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "Influence structures are cheaper and can be built on grass or trees, but can be attacked\n" +
                                                                              "/destroyed. They can, however, be upgraded to increase nearby influence, and can attack\n" +
                                                                              "  further once upgraded. They provide 1 to 3 influence to neighboring tiles and cost\n" +
                                                                              "5 wood/stone/clay, 2 wheat for level 1, 10 wood/clay, 5 stone, and 2 wheat for level 2,\n" +
                                                                              "              and 20 wood, 10 clay, 15 stone, 5 wheat for level 3.", new Vector2(128, yRoot), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texGrass, new Rectangle(390, yRoot + spriteFont.LineSpacing * 6, 120, 120), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texTree, new Rectangle(740, yRoot + spriteFont.LineSpacing * 6, 120, 120), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texInfluenceO1, new Rectangle(275, yRoot + spriteFont.LineSpacing * 6 + 120, 200, 200), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texInfluenceO2, new Rectangle(525, yRoot + spriteFont.LineSpacing * 6 + 120, 200, 200), Color.White);
                        this.ScreenManager.SpriteBatch.Draw(texInfluenceO3, new Rectangle(775, yRoot + spriteFont.LineSpacing * 6 + 120, 200, 200), Color.White);
                        
                        break;
                    case 5:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The first player is randomly selected when the game loads, and each player takes turns \n" +
                                                                              "   placing one tower. After the towers are built, the play continues with each player\n" +
                                                                              "   upgrading and attacking as possible. Towers can only be built on or near existing \n" +
                                                                              " influence; multiple towers combine their influence. Competing towers on opposite sides\n" +
                                                                              "reduce their opponent's influence in the area (as below, red and blue negate influence\n" +
                                                                              "          in the middle where it is equal, and win where one side has more).", new Vector2(130, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "The first player is randomly selected when the game loads, and each player takes turns \n" +
                                                                              "   placing one tower. After the towers are built, the play continues with each player\n" +
                                                                              "   upgrading and attacking as possible. Towers can only be built on or near existing \n" +
                                                                              " influence; multiple towers combine their influence. Competing towers on opposite sides\n" +
                                                                              "reduce their opponent's influence in the area (as below, red and blue negate influence\n" +
                                                                              "          in the middle where it is equal, and win where one side has more).", new Vector2(128, yRoot), Color.White);

                        this.ScreenManager.SpriteBatch.Draw(texCompare, new Rectangle(360, yRoot + spriteFont.LineSpacing * 6 + 20, 560, 400), Color.White);
                        break;
                    case 6:
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "\nA few things to note: \n" +
                                                                              "    > Sand and water block influence, but water can be bypassed by building a bridge.\n" +
                                                                              "    > Mountains reduce distant influence, so you may need to build more/better towers. \n" +
                                                                              "    > Each turn, a new resource is offered for trade; this is opened/closed by \n" +
                                                                              "      up/down on the DPad. Only the current player can trade!\n" +
                                                                              "    > Players with all towers destroyed will be allowed to restart with a minimum\n" +
                                                                              "      resource allotment.\n" +
                                                                              "    > A good defense is the best offense, so use bottlenecks and the occasional\n" +
                                                                              "      expensive defensive tower.\n" +
                                                                              "    > The camera can be rotated and zoomed using the shoulders/bumpers.\n" +
                                                                              "      It can be reset by clicking down on the left thumbstick." +
                                                                              "\n\n\n\n                                              Good luck!", new Vector2(130, yRoot + 2), Color.Black);
                        this.ScreenManager.SpriteBatch.DrawString(spriteFont, "\nA few things to note: \n" +
                                                                              "    > Sand and water block influence, but water can be bypassed by building a bridge.\n" +
                                                                              "    > Mountains reduce distant influence, so you may need to build more/better towers. \n" +
                                                                              "    > Each turn, a new resource is offered for trade; this is opened/closed by \n" +
                                                                              "      up/down on the DPad. Only the current player can trade!\n" +
                                                                              "    > Players with all towers destroyed will be allowed to restart with a minimum\n" +
                                                                              "      resource allotment.\n" +
                                                                              "    > A good defense is the best offense, so use bottlenecks and the occasional\n" +
                                                                              "      expensive defensive tower.\n" +
                                                                              "    > The camera can be rotated and zoomed using the shoulders/bumpers.\n" +
                                                                              "      It can be reset by clicking down on the left thumbstick." +
                                                                              "\n\n\n\n                                              Good luck!", new Vector2(128, yRoot), Color.White);
                        break;
                    default:
                        currentPage = 1;
                        break;
                }

                // Draw count at the bottom
                this.ScreenManager.SpriteBatch.DrawString(spriteFont, "<   " + currentPage + "/" + maxPages + "   >", new Vector2((ScreenManager.GraphicsDevice.Viewport.Width / 2) - (spriteFont.MeasureString("<   1/1   >").X / 2) + 2, 62), Color.Black);
                this.ScreenManager.SpriteBatch.DrawString(spriteFont, "<   " + currentPage + "/" + maxPages + "   >", new Vector2((ScreenManager.GraphicsDevice.Viewport.Width / 2) - (spriteFont.MeasureString("<   1/1   >").X / 2), 60), Color.White);

                //this.ScreenManager.SpriteBatch.Draw(gradient, new Rectangle(0, 0, 128, 720), new Color(0.5f, 0.5f, 0.5f, 0.5f));
                //this.ScreenManager.SpriteBatch.Draw(gradient, new Rectangle(0, 0, 1280, 72), new Color(0.5f, 0.5f, 0.5f, 0.5f));
                //this.ScreenManager.SpriteBatch.Draw(gradient, new Rectangle(0, 648, 1280, 72), new Color(0.5f, 0.5f, 0.5f, 0.5f));
                //this.ScreenManager.SpriteBatch.Draw(gradient, new Rectangle(1152, 0, 128, 720), new Color(0.5f, 0.5f, 0.5f, 0.5f));

                this.ScreenManager.SpriteBatch.End();

                base.Draw(gameTime);
            }
        }


        #endregion

        #region Handle Input

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            if ((input.CurrentKeyboardStates[(int)this.ControllingPlayer].IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadRight) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.LeftThumbstickRight)) && timeSinceIncrement > 0.3f)
            {
                timeSinceIncrement = 0.0f;

                currentPage++;

                if (currentPage > maxPages)
                    currentPage = 1;
            }
            else if ((input.CurrentKeyboardStates[(int)this.ControllingPlayer].IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadLeft) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.LeftThumbstickLeft)) && timeSinceIncrement > 0.3f)
            {
                timeSinceIncrement = 0.0f;

                currentPage--;

                if (currentPage < 1)
                    currentPage = maxPages;
            }
            else if (input.CurrentKeyboardStates[(int)this.ControllingPlayer].IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Back) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.B) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.Back))
            {
                //this.ScreenManager.currentBackground = 0;
                exiting = true;
                this.ExitScreen();
            }

            base.HandleInput(gameTime, input);
        }

        #endregion
    }
}
